package net.ojbk.sso.center.web.handlers;

import net.ojbk.sso.center.beans.po.ClientWebsite;
import net.ojbk.sso.center.beans.po.User;
import net.ojbk.sso.center.beans.pojo.ClientWebSystem;
import net.ojbk.sso.center.beans.pojo.TokenSystem;
import net.ojbk.sso.center.beans.vo.VO_LoggedWebsites;
import net.ojbk.sso.center.service.LoginService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/3/24
 * Time: 11:35
 * Description: No Description
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    private static Logger logger = Logger.getLogger(LoginController.class);
    @Autowired
    private LoginService loginService;
    @Autowired
    private TokenSystem tokenSystem;

    // 1.1 获取Login的所有网站
    @RequestMapping(value = "websites", method = RequestMethod.GET)
    public ResponseEntity<Object> getLoginedWeb(HttpSession session, String token) throws Exception {
        if (token == null || token.equals(""))
            token = tokenSystem.getToken_ByHttpSession(session);
        return new ResponseEntity<Object>(tokenSystem.getAllloggedWeb(token), HttpStatus.OK);
    }

    // 1.2 获取已登录的token
    @RequestMapping(value = "token", method = RequestMethod.GET)
    public ResponseEntity<Object> getToken(HttpSession session) throws Exception {
        return new ResponseEntity<>(tokenSystem.getToken_ByHttpSession(session), HttpStatus.OK);
    }

    // 1.3 使用账号密码登陆
    @RequestMapping(value = "", method = RequestMethod.POST, params = {"uid", "pwd"})
    public ResponseEntity<Object> login(HttpSession session, String uid, String pwd) {
        if (session.getAttribute("user") != null)
            return new ResponseEntity<>("已处于登陆状态", HttpStatus.OK);

        User user = loginService.login(uid, pwd, session);
        if (user != null) {
            session.setAttribute("user", user);
            return new ResponseEntity<>("成功", HttpStatus.OK);
        }
        return new ResponseEntity<>("失败", HttpStatus.BAD_REQUEST);
    }

    // 1.4 获取已登录的uid
    @RequestMapping(value = "uid", method = RequestMethod.GET)
    public ResponseEntity<Object> getUid(HttpSession session, String token) throws Exception {
        logger.debug("用户调用获取uid接口，token为：" + token);
        if (token != null && !token.equals(""))
            session = tokenSystem.getHttpSession_ByToken(token);
        return new ResponseEntity<>(loginService.getUid(session), HttpStatus.OK);
    }

    // 1.5 注销所有网站
    @RequestMapping(value = "websites", method = RequestMethod.DELETE)
    public ResponseEntity<Object> logout(HttpSession session, String token) throws Exception {
        if (token == null || token.equals(""))
            token = tokenSystem.getToken_ByHttpSession(session);
        // April 9 2019 19:47 添加支持token参数，考虑到当用户对于center的cookies突然失效

        tokenSystem.logoutAllClientWebsite(token);
        session.removeAttribute("user");
        return new ResponseEntity<>("success", HttpStatus.OK);

    }
}
